package com.dianrui.operation.activity;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;


import com.dianrui.operation.Constant;
import com.dianrui.operation.R;

import butterknife.BindView;
import butterknife.OnClick;

public class WebViewActivity extends BaseActivity {
    private LinearLayout ll_control_error;
    private boolean isSuccess = false;
    private boolean isError = false;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.webview)
    WebView webview;
    @BindView(R.id.act_progressbar)
    ProgressBar mProgressBar;

    @BindView(R.id.show_webview_layout)
    LinearLayout showWebviewLayout;

    @Override
    public int getContentViewId() {
        return R.layout.webview;
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public void initView() {
        webview.loadUrl(Constant.explain_url);
        WebSettings webSetting = webview.getSettings();
        webSetting.setJavaScriptEnabled(true);
        webSetting.setUseWideViewPort(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSetting.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        ll_control_error = findViewById(R.id.ll_control_error);
        Button errorBtn=ll_control_error.findViewById(R.id.online_error_btn_retry);
        TextView errorTitle=ll_control_error.findViewById(R.id.title);
        ImageView backimg=ll_control_error.findViewById(R.id.back);
        errorTitle.setText("找不到网页");
        backimg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        errorBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ll_control_error.setVisibility(View.GONE);
                showWebviewLayout.setVisibility(View.VISIBLE);
                webview.reload();
            }
        });

        webview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                if (mProgressBar != null) {
                    mProgressBar.setVisibility(View.VISIBLE);
                }



            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (mProgressBar.getProgress() == 100) {
                    mProgressBar.setVisibility(View.GONE);
                }
                if (!isError) {
                    isSuccess = true;
                    //回调成功后的相关操作
                    ll_control_error.setVisibility(View.GONE);
                    showWebviewLayout.setVisibility(View.VISIBLE);
                    webview.setVisibility(View.VISIBLE);
                } else {
                    isError = false;
                    webview.setVisibility(View.GONE);
                    showWebviewLayout.setVisibility(View.GONE);
                    ll_control_error.setVisibility(View.VISIBLE);
                }

            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    return;
                }
                isError = true;
                isSuccess = false;
                //6.0以下执行
                webview.setVisibility(View.GONE);
                showWebviewLayout.setVisibility(View.GONE);
                ll_control_error.setVisibility(View.VISIBLE);

            }

            //加载错误的时候会回调
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                isError = true;
                isSuccess = false;
                //6.0以上执行
                webview.setVisibility(View.GONE);
                showWebviewLayout.setVisibility(View.GONE);
                ll_control_error.setVisibility(View.VISIBLE);

            }


            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        webview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                //设置进度条的最大值
                if (mProgressBar != null) {
                    mProgressBar.setProgress(newProgress);
                }
            }

            @Override
            public void onReceivedTitle(WebView view, String titles) {
                super.onReceivedTitle(view, titles);
                title.setText(titles);
            }
        });
    }

    @Override
    public void initData() {}

    @Override
    public void finish() {
        ViewGroup view = (ViewGroup) getWindow().getDecorView();
        view.removeAllViews();
        super.finish();
    }


    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (webview != null) {
            webview.onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (webview != null) {
            webview.onPause();
        }
    }


    @OnClick({R.id.back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;

                default:
                    break;

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (webview != null) {
            webview.clearHistory();
            webview.removeAllViews();
            webview.destroy();
        }
    }


}
